const cookie = {
  // eslint-disable-next-line consistent-return
  getCookie: (cname: string) => {
    const name = `${cname}=`;
    const ca = document.cookie.split(';');
    for (let i = 0; i < ca.length; i += 1) {
      const c = ca[i].trim();
      if (c.indexOf(name) === 0) {
        return c.substring(name.length, c.length);
      }
    }
  },
  // setCookie
  setCookie: (cname: string, cvalue: string, exdays: number) => {
    const d = new Date();
    d.setTime(d.getTime() + exdays * 24 * 60 * 60 * 1000);
    const expires = `expires=${d.toUTCString()}`;
    document.cookie = `${cname}=${cvalue};${expires};path=/`;
  },
  removeCookie: (cname: string) => {
    document.cookie = `${cname}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;`;
  },

  getAllCookies: () => {
    const cookies = document.cookie.split(';');
    const result:Record<string, any> = {};
    cookies.forEach((cks) => {
      const [key, value] = cks.split('=');
      result[key.trim()] = value;
    });
    return result;
  },
};

export default cookie;
